{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "caa7e380",
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'sklearn'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "Input \u001b[1;32mIn [1]\u001b[0m, in \u001b[0;36m<cell line: 11>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      9\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mmatplotlib\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpyplot\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m MultipleLocator\n\u001b[0;32m     10\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mos\u001b[39;00m\n\u001b[1;32m---> 11\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msklearn\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m tree\n\u001b[0;32m     12\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msklearn\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mutils\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m shuffle\n\u001b[0;32m     13\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mgraphviz\u001b[39;00m\n",
      "\u001b[1;31mModuleNotFoundError\u001b[0m: No module named 'sklearn'"
     ]
    }
   ],
   "source": [
    "from data import Dataset\n",
    "import pandas as pd\n",
    "from datetime import datetime,timedelta,date\n",
    "import numpy as np\n",
    "from random import uniform\n",
    "from tushare import pro_api\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.dates as mdates\n",
    "from matplotlib.pyplot import MultipleLocator\n",
    "import os\n",
    "from sklearn import tree\n",
    "from sklearn.utils import shuffle\n",
    "import graphviz\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.model_selection import cross_val_score\n",
    "from sklearn.preprocessing import MinMaxScaler\n",
    "from sklearn.ensemble import AdaBoostClassifier\n",
    "from sklearn.ensemble import GradientBoostingClassifier\n",
    "from speculation_rule.BackTest import BackTest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2129ff92",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(40185, 97)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv(r'五分钟线_data\\train_data\\5min_train_2023_0.csv')\n",
    "data = shuffle(data)\n",
    "data_1 = data[data['target']==1]\n",
    "data_0 = data[data['target']!=1].iloc[:20000,:]\n",
    "data = pd.concat([data_1,data_0])\n",
    "data = shuffle(data)\n",
    "data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "064b0d92",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_data = data.iloc[:,:-1]\n",
    "target_data = data.iloc[:,-1]\n",
    "\n",
    "#train_data = MinMaxScaler().fit_transform(train_data)\n",
    "\n",
    "Xtrain, Xtest, Ytrain, Ytest = train_test_split(train_data, target_data, test_size=0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "6409992a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.548836630583551"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rfc = RandomForestClassifier(n_jobs=4,max_features=48,max_depth=2)\n",
    "rfc = rfc.fit(Xtrain,Ytrain)\n",
    "score_r = rfc.score(Xtest,Ytest)\n",
    "score_r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a774904d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9354473386183465"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gbc = GradientBoostingClassifier()\n",
    "gbc.fit(Xtrain,Ytrain)\n",
    "score = gbc.score(Xtest,Ytest)\n",
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "5d5d427d",
   "metadata": {},
   "outputs": [],
   "source": [
    "prd_data = pd.read_csv('日线_成交量_星期_data/train_data/0120_2122.csv')\n",
    "prd_code = prd_data.iloc[:,-1]\n",
    "prd_data = prd_data.iloc[:,:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ee019a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "prd_rs = rfc.predict(prd_data)\n",
    "prd_rs = prd_rs.astype('bool')\n",
    "up_code = prd_code[prd_rs]\n",
    "up_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "81e39a66",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>trade_date</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>change</th>\n",
       "      <th>pct_chg</th>\n",
       "      <th>vol</th>\n",
       "      <th>amount</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>836957.BJ</td>\n",
       "      <td>20230130</td>\n",
       "      <td>5.59</td>\n",
       "      <td>5.65</td>\n",
       "      <td>5.59</td>\n",
       "      <td>5.64</td>\n",
       "      <td>5.59</td>\n",
       "      <td>0.05</td>\n",
       "      <td>0.8945</td>\n",
       "      <td>2732.43</td>\n",
       "      <td>1535.982</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>430139.BJ</td>\n",
       "      <td>20230130</td>\n",
       "      <td>9.94</td>\n",
       "      <td>9.98</td>\n",
       "      <td>9.80</td>\n",
       "      <td>9.94</td>\n",
       "      <td>9.75</td>\n",
       "      <td>0.19</td>\n",
       "      <td>1.9487</td>\n",
       "      <td>3981.44</td>\n",
       "      <td>3936.572</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>838670.BJ</td>\n",
       "      <td>20230130</td>\n",
       "      <td>17.54</td>\n",
       "      <td>17.93</td>\n",
       "      <td>17.54</td>\n",
       "      <td>17.83</td>\n",
       "      <td>17.47</td>\n",
       "      <td>0.36</td>\n",
       "      <td>2.0607</td>\n",
       "      <td>1483.19</td>\n",
       "      <td>2636.530</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>871857.BJ</td>\n",
       "      <td>20230130</td>\n",
       "      <td>8.50</td>\n",
       "      <td>8.50</td>\n",
       "      <td>8.46</td>\n",
       "      <td>8.49</td>\n",
       "      <td>8.44</td>\n",
       "      <td>0.05</td>\n",
       "      <td>0.5924</td>\n",
       "      <td>379.52</td>\n",
       "      <td>322.243</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>835179.BJ</td>\n",
       "      <td>20230130</td>\n",
       "      <td>19.17</td>\n",
       "      <td>19.53</td>\n",
       "      <td>19.17</td>\n",
       "      <td>19.49</td>\n",
       "      <td>19.07</td>\n",
       "      <td>0.42</td>\n",
       "      <td>2.2024</td>\n",
       "      <td>1467.19</td>\n",
       "      <td>2851.914</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     ts_code trade_date   open   high    low  close  pre_close  change  \\\n",
       "0  836957.BJ   20230130   5.59   5.65   5.59   5.64       5.59    0.05   \n",
       "1  430139.BJ   20230130   9.94   9.98   9.80   9.94       9.75    0.19   \n",
       "2  838670.BJ   20230130  17.54  17.93  17.54  17.83      17.47    0.36   \n",
       "3  871857.BJ   20230130   8.50   8.50   8.46   8.49       8.44    0.05   \n",
       "4  835179.BJ   20230130  19.17  19.53  19.17  19.49      19.07    0.42   \n",
       "\n",
       "   pct_chg      vol    amount  \n",
       "0   0.8945  2732.43  1535.982  \n",
       "1   1.9487  3981.44  3936.572  \n",
       "2   2.0607  1483.19  2636.530  \n",
       "3   0.5924   379.52   322.243  \n",
       "4   2.2024  1467.19  2851.914  "
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pro = pro_api('9078d7e9d562d1ad22630515a177cf57a9c245fefb09bd62f5b07b20')\n",
    "today = pro.daily(trade_date='20230130')\n",
    "today.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "6af5aadd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tup_code = today[today['pct_chg']>0]['ts_code'].tolist()\n",
    "up = 0\n",
    "for i in up_code:\n",
    "    if i in tup_code:\n",
    "        up += 1\n",
    "up/len(up_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "ba0148d6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "47334aef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(19497, 67)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv('日线_成交量_星期_data/train_data/79596.csv')\n",
    "data = shuffle(data)\n",
    "data_1 = data[data['target']==1]\n",
    "data_0 = data[data['target']!=1].iloc[:10000,:]\n",
    "data = pd.concat([data_1,data_0])\n",
    "data = shuffle(data)\n",
    "data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "fd3e11f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "train_data = data.iloc[:,:-1]\n",
    "target_data = data.iloc[:,-1]\n",
    "train_data = StandardScaler().fit_transform(train_data)\n",
    "Xtrain_2, Xtest_2, Ytrain_2, Ytest_2 = train_test_split(train_data, target_data, test_size=0.3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "4fa2c4ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7581196581196581"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rfc_2 = RandomForestClassifier()\n",
    "rfc_2 = rfc_2.fit(Xtrain_2,Ytrain_2)\n",
    "score_r = rfc_2.score(Xtest_2,Ytest_2)\n",
    "score_r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "94701eb3",
   "metadata": {},
   "outputs": [],
   "source": [
    "prd1=rfc.predict(Xtest)\n",
    "prd2=rfc_2.predict(Xtest)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3e2d028b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from speculation_rule.BackTest import BackTest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4c697c48",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "14.97"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test = BackTest('日线_data')\n",
    "df = test.get_data('000001.SZ','20230117')\n",
    "df.iloc[0]['close']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "1a682b8f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "position = {'name':'job'}\n",
    "'job'not in position.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72c8fe45",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "vscode": {
   "interpreter": {
    "hash": "570feb405e2e27c949193ac68f46852414290d515b0ba6e5d90d076ed2284471"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
